<?php
/**
 * Author: Nguyen Hoai Tan (nguoiao007@gmail.com)
 * 
 * @category   default 
 * @copyright  Copyright (c) 2009 visualidea.org
 * @license    http://license.visualidea.org
 * @version    v 1.0 2009-04-15
 * 
 */
require_once 'modules/user/models/User.php';
require_once 'modules/content/models/Content.php';
require_once 'modules/mail/models/Mail.php';

class user_IndexController extends Nine_Controller_Action
{
    /**
     * Forgot Username or Password
     */
	public function forgotUsernameOrPasswordAction()
    {
        $this->view->headTitle('Forgot username or password');
        $this->view->bannerImage = 12;
        $this->view->menuId = 'assistance';
        
        $objContent = new Models_Content();
        $objUser = new Models_User();
        
        /**
         * Get email param
         */
        $email = $this->_getParam('email', false);
        
        if (false != $email) {
            $user = @reset($objUser->getByColumns(array('email=?'=>$email))->toArray());
            
            if (false == $user) {
                 $this->session->forgotMessage = array(
                								'success'	=>	false,
                								'message'	=>	Nine_Language::translate("Email doesn't exist. Please try with correct email.")
                							);
            } else {
                /**
                 * Create random code
                 */
                $randomCode = $objUser->generateActiveCode(20);
                $link = Nine_Registry::getConfig('liveSite'). 'access/index/login/username/' . $user['username'] . '/password/' . $randomCode;
                /**
                 * Update user
                 */
                $data = array(
                    'forgot_password_code' => md5($randomCode),
                    'forgot_password_expired_date' => time() + Nine_Registry::getConfig('forgotPasswordExpiredTime')
                );
                
                $objUser->update($data, array('user_id=?' => $user['user_id']));
			
                /**
                 * Send mail
                 */
                $data = array(
                        'email' => $email,
                        'link' => $link,
                		'new_password'	=>	$randomCode,
                        'username' => $user['username'],
                        'full_name' => $user['full_name']
                );
                $objMail = new Models_Mail();
                $objMail->sendHtmlMail('forgot_password', $data, $email);
                
                $this->session->forgotMessage = array(
                								'success'	=>	true,
                								'message'	=>	Nine_Language::translate("Please check your email to see your username and new password. Please note that new password is only valid in 24 hours. You can still use old password if you don't use new password.")
                							);
        	}
    	}
    	
        /**
         * Get content
         */
        $contentLeft = @$objContent->getContentByGid(57)->toArray();
        $tmp = explode("||", $contentLeft['images']);
        $contentLeft['main_image'] = Nine_Function::getThumbImage(@$tmp[0],207,143);
        
        $contentCenter = @$objContent->getContentByGid(55)->toArray();
        
        $this->view->contentLeft = $contentLeft;
        $this->view->contentCenter = $contentCenter;
    	
	    $this->view->email = $email;
	    
	    $this->view->forgotMessage = $this->session->forgotMessage;
	    $this->session->forgotMessage = null;    
	        
    }
}